package com.jlastu.znb.gwc;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;
import com.jlastu.znb.adapter.CartAdapter;

public class CartActivity extends AppCompatActivity {
    private CartAdapter cartAdapter;
    private ListView cartListView;
    private CartManager cartManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart); // 使用购物车布局文件

        cartManager = new CartManager(this);
        cartListView = findViewById(R.id.cart_list_view);
        cartAdapter = new CartAdapter(this, cartManager.getCartItems());
        cartListView.setAdapter(cartAdapter);

        // 设置购物车列表项点击事件，用于删除操作
        cartListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CartItem itemToRemove = cartAdapter.getItem(position);
                cartManager.removeFromCart(itemToRemove.getId());
                cartAdapter.remove(itemToRemove);
                cartAdapter.notifyDataSetChanged();
            }
        });
    }
}